home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / hpdio.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  4KB  |  172 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '0.1'
  5. __title__ = 'Services and Status System Tray Device I/O Child Process'
  6. __mod__ = 'hpdio'
  7. __doc__ = 'Provides device I/O process isolation for system tray application.'
  8. import sys
  9. import struct
  10. import os
  11. import time
  12. import Queue
  13. import select
  14. from cPickle import dumps, HIGHEST_PROTOCOL
  15. from base.g import *
  16. from base.codes import *
  17. from base import utils, device, status, models
  18.  
  19. try:
  20.     from dbus import lowlevel, SessionBus
  21. except ImportError:
  22.     log.error('dbus failed to load (python-dbus ver. 0.80+ required). Exiting...')
  23.     sys.exit(1)
  24.  
  25. PIPE_BUF = 4096
  26. session_bus = None
  27. (r2, w3) = (None, None)
  28. devices = { }
  29.  
  30. def send_message(device_uri, event_code, bytes_written = 0):
  31.     args = [
  32.         device_uri,
  33.         '',
  34.         event_code,
  35.         prop.username,
  36.         0,
  37.         '',
  38.         '',
  39.         bytes_written]
  40.     msg = lowlevel.SignalMessage('/', 'com.hplip.StatusService', 'Event')
  41.     msg.append(signature = 'ssisissi', *args)
  42.     SessionBus().send_message(msg)
  43.  
  44.  
  45. def run(read_pipe2 = None, write_pipe3 = None):
  46.     global r2, w3
  47.     tmp_dir = '/tmp'
  48.     os.umask(73)
  49.     
  50.     try:
  51.         log.set_module('hp-systray(hpdio)')
  52.         log.debug('PID=%d' % os.getpid())
  53.         r2 = read_pipe2
  54.         w3 = write_pipe3
  55.         fmt = '64s64sI32sI64sf'
  56.         fmt_size = struct.calcsize(fmt)
  57.         response = { }
  58.         dev = None
  59.         m = ''
  60.         while True:
  61.             
  62.             try:
  63.                 (r, w, e) = select.select([
  64.                     r2], [], [
  65.                     r2], 1)
  66.             except KeyboardInterrupt:
  67.                 break
  68.             except select.error:
  69.                 e = None
  70.                 if e[0] == errno.EINTR:
  71.                     continue
  72.                 else:
  73.                     break
  74.             except:
  75.                 e[0] == errno.EINTR
  76.  
  77.             if not r:
  78.                 continue
  79.             
  80.             if e:
  81.                 break
  82.             
  83.             m = ''.join([
  84.                 m,
  85.                 os.read(r2, fmt_size)])
  86.             if not m:
  87.                 break
  88.             
  89.             while len(m) >= fmt_size:
  90.                 response.clear()
  91.                 event = device.Event(*struct.unpack(fmt, m[:fmt_size]))
  92.                 m = m[fmt_size:]
  93.                 action = event.event_code
  94.                 device_uri = event.device_uri
  95.                 log.debug('Handling event...')
  96.                 event.debug()
  97.                 send_message(device_uri, EVENT_DEVICE_UPDATE_ACTIVE)
  98.                 if action in (EVENT_DEVICE_UPDATE_REQUESTED, EVENT_POLLING_REQUEST):
  99.                     
  100.                     try:
  101.                         dev = devices[device_uri]
  102.                     except KeyError:
  103.                         dev = devices[device_uri] = device.Device(device_uri, disable_dbus = True)
  104.  
  105.                     
  106.                     try:
  107.                         dev.open()
  108.                     except Error:
  109.                         e = None
  110.                         log.error(e.msg)
  111.                         response = {
  112.                             'error-state': ERROR_STATE_ERROR,
  113.                             'device-state': DEVICE_STATE_NOT_FOUND,
  114.                             'status-code': EVENT_ERROR_DEVICE_IO_ERROR }
  115.  
  116.                     if dev.device_state == DEVICE_STATE_NOT_FOUND:
  117.                         dev.error_state = ERROR_STATE_ERROR
  118.                     elif action == EVENT_DEVICE_UPDATE_REQUESTED:
  119.                         
  120.                         try:
  121.                             dev.queryDevice()
  122.                         except Error:
  123.                             e = None
  124.                             log.error('Query device error (%s).' % e.msg)
  125.                             dev.error_state = ERROR_STATE_ERROR
  126.                             dev.status_code = EVENT_ERROR_DEVICE_IO_ERROR
  127.  
  128.                         response = dev.dq
  129.                         log.debug('Device state = %d' % dev.device_state)
  130.                         log.debug('Status code = %d' % dev.status_code)
  131.                         log.debug('Error state = %d' % dev.error_state)
  132.                     else:
  133.                         
  134.                         try:
  135.                             dev.pollDevice()
  136.                         except Error:
  137.                             e = None
  138.                             log.error('Poll device error (%s).' % e.msg)
  139.                             dev.error_state = ERROR_STATE_ERROR
  140.  
  141.                         response = {
  142.                             'test': 1 }
  143.                     if dev is not None:
  144.                         dev.close()
  145.                     
  146.                 elif action == EVENT_USER_CONFIGURATION_CHANGED:
  147.                     pass
  148.                 elif action == EVENT_SYSTEMTRAY_EXIT:
  149.                     log.debug('Exiting')
  150.                     sys.exit(1)
  151.                 
  152.                 send_message(device_uri, EVENT_DEVICE_UPDATE_INACTIVE)
  153.                 if action == EVENT_DEVICE_UPDATE_REQUESTED:
  154.                     data = dumps(response, HIGHEST_PROTOCOL)
  155.                     log.debug('Sending data through pipe to hpssd...')
  156.                     total_written = 0
  157.                     while True:
  158.                         total_written += os.write(w3, data[:PIPE_BUF])
  159.                         data = data[PIPE_BUF:]
  160.                         if not data:
  161.                             break
  162.                             continue
  163.                     log.debug('Wrote %d bytes' % total_written)
  164.                     send_message(device_uri, EVENT_DEVICE_UPDATE_REPLY, total_written)
  165.                     continue
  166.                 if action == EVENT_POLLING_REQUEST:
  167.                     continue
  168.     except KeyboardInterrupt:
  169.         log.debug('Ctrl-C: Exiting...')
  170.  
  171.  
  172.